package hdpdhl.com.czxy.hdpdhl;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {
    //轮播图的控件
    private ViewPager viewPager_hdp;
    private LinearLayout indicator_hdp;
    private PagerAdapter adapter;
    private List<Fragment> fragments = new ArrayList<Fragment>();
    boolean isRunning = false;
    //导航栏的控件
    private ViewPager viewPager_dhl;
    private ViewPagerIndicator indicator_dhl;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mList;
    private List<String> mDatas;
    private int itemCount = 4;
    //fragment的内容
    private TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager_hdp = (ViewPager) findViewById(R.id.viewpager_hdp);
        viewPager_hdp.setCurrentItem(5000000);
        //viewPager_hdp.setOffscreenPageLimit(1);
        indicator_hdp = (LinearLayout) findViewById(R.id.indicator_hdp);

        for (int i = 0; i < 5; i++) {
            ContentFragment fragment = new ContentFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("index", i);
            fragment.setArguments(bundle);
            fragments.add(fragment);
        }
        adapter = new ViewPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager_hdp.setAdapter(adapter);
        initIndicator();
        viewPager_hdp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int index, float positionOffset, int positionOffsetPixels) {
                for (int i = 0; i < fragments.size(); i++) {
                    indicator_hdp.getChildAt(i).setBackgroundResource(index == i ? R.drawable.dot_fox : R.drawable.dot_normal);
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //轮播线程
        new Thread() {
            @Override
            public void run() {
                isRunning = true;
                while (isRunning) {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 往下跳一位
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            int i = viewPager_hdp.getCurrentItem() + 1;
                            if (viewPager_hdp.getCurrentItem() == 4) {
                                viewPager_hdp.setCurrentItem(0);
                                i = 0;
                            }
                            System.out.println("设置当前位置: " + viewPager_hdp.getCurrentItem());
                            viewPager_hdp.setCurrentItem(i);
                        }

                    });
                }
            }
        }.start();

        //导航栏初始化----------------------------------------------//
        viewPager_dhl = (ViewPager) findViewById(R.id.vp_dhl);
        indicator_dhl = (ViewPagerIndicator) findViewById(R.id.indicator_dhl);
        mList = new ArrayList<Fragment>();
        for (int i = 0; i < itemCount; i++) {
            Fragment fragment = new MeFragment();
            mList.add(fragment);
        }

        mDatas = new ArrayList<>();
        mDatas.add("悬赏栏");
        mDatas.add("未悬赏栏");
        mDatas.add("我要提问");
        mDatas.add("用户反馈");


        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mList.get(position);
            }

            @Override
            public int getCount() {
                return mList.size();
            }
        };

        viewPager_dhl.setAdapter(mAdapter);
        //将viewpager与indicator绑定
        indicator_dhl.setDatas(mDatas);
        indicator_dhl.setViewPager(viewPager_dhl);
        //导航栏初始化结束------------------------------------------//

//        textView = (TextView) findViewById(R.id.tv);
//        textView.setInputType(96);
        /**
         * 设置listview的点击事件
         */


    }

    private void initIndicator() {
        int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, getResources().getDisplayMetrics());
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width, width);
        lp.rightMargin = 5;
        for (int i = 0; i < fragments.size(); i++) {
            View view = new View(this);
            view.setId(i);
            view.setBackgroundResource(i == 0 ? R.drawable.dot_fox : R.drawable.dot_normal);
            view.setLayoutParams(lp);
            indicator_hdp.addView(view, i);
        }
    }
}
